package com.ronglian.register_mini_program.service.impl;

import com.github.pagehelper.PageHelper;
import com.ronglian.register_mini_program.dao.RegisterOrderMapper;
import com.ronglian.register_mini_program.model.RegisterOrder;
import com.ronglian.register_mini_program.model.RegisterOrderExample;
import com.ronglian.register_mini_program.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("OrderService")
public class OrderServiceImpl implements OrderService {

    @Autowired
    RegisterOrderMapper orderMapper;

    @Override
    public List<RegisterOrder> findOrderListWithActivityId(Integer activityId) {
        RegisterOrderExample orderExample = new RegisterOrderExample();
        RegisterOrderExample.Criteria oc = orderExample.createCriteria();
        oc.andActivity_idEqualTo(activityId);
        orderExample.setOrderByClause("create_time desc");
        List<RegisterOrder> orders = orderMapper.selectByExample(orderExample);
        if (orders != null && orders.size() > 0) {
            return orders;
        } else {
            return null;
        }
    }

    @Override
    public List<RegisterOrder> findAll(int page_num, int page_size) {
        PageHelper.startPage(page_num, page_size);
        return orderMapper.selectByExample(new RegisterOrderExample());
    }

    @Override
    public RegisterOrder findOrderWithOpenIdAndActivityId(String open_id, Integer activityId) {
        RegisterOrderExample orderExample = new RegisterOrderExample();
        RegisterOrderExample.Criteria oc = orderExample.createCriteria();
        oc.andActivity_idEqualTo(activityId);
        oc.andOpen_idEqualTo(open_id);
        List<RegisterOrder> orders = orderMapper.selectByExample(orderExample);
        if (orders != null && orders.size() > 0) {
            return orders.get(0);
        }
        return null;
    }

}
